/*
 * @Descripttion: 
 * @version: 请写项目版本
 * @Author: @cdm
 * @Date: 2021-09-08 13:54:32
 * @LastEditors: @cdm
 * @LastEditTime: 2021-12-20 14:31:38
 * No pains no gains
 */
import { game } from "../../../../script/game/control/game";
import { ListViewAdapter } from "../../../../script/util/ListView/ListViewAdapter";
import { ccs } from "../../../../script/util/services";
import { TouchExtend } from "../../../../script/util/TouchExd";
import { touchExd } from "../../../../script/util/TouchExtend";
import { WinBase } from "../../../../script/wins/WinBase";

const {ccclass, property} = cc._decorator;

@ccclass
export default class NewClass extends WinBase {
    @property(cc.Node)
    list: cc.Node = null;
    
    start () {
        this.setData();
        
    }
    setData() {
        let action = this.list.children;
        let ads = game.playerMgr.ads;
        action.forEach((val, idx) => {
            val.getChildByName("leftCount").getComponent(cc.RichText).string = `剩余次数：${ads[idx]}`
            // val.getChildByName("leftCount").color =  new cc.Color(43,38,38);
            TouchExtend.touchEvent(val, cc.Node.EventType.TOUCH_END, () => {
                    if(ads[idx] <= 0) {return};
                    ccs.AD.showVideoAd((res) => {
                        this.handlerEvent(idx);
                    })
            })
                
        })
    }
    randomMoney(min, max) {
        return ccs.gf.randomInt(min, max);
    }
    handlerEvent(idx) {
        if(idx == 0 ) {
            let money = this.randomMoney(100, 10000);
            game.playerMgr.calMoney(money);
            ccs.winMgr.showWin("toast", {msg: `投资获得${money}元投资`});
        } else if(idx == 1) {
            let arr = ['health', 'maxPower', 'iq', 'eq', 'charm', 'money', 'learn', 'lifeTime' ];
            let key = arr[ccs.gf.randomInt(0, arr.length - 1)];
            let num = ccs.gf.randomInt(1, 3);
            game.playerMgr.heroInfo[key] += num;
            ccs.winMgr.showWin("toast", {msg: `投资获得${ccs.gf.getEffectString({[key]: num})}属性`});
        }else if(idx == 2) {
            game.playerMgr.heroInfo.lifeTime += 1;
            ccs.winMgr.showWin("toast", {msg: `投资获得一个月寿命`});
        }else {
            let money = this.randomMoney(1000, 10000);
            game.playerMgr.calMoney(money);
            ccs.winMgr.showWin("toast", {msg: `投资获得${money}元投资`});
        }
        game.playerMgr.ads[idx] --;
        cc.log(game.playerMgr.ads[idx]);
        game.playerMgr.achieveFinish('ad', 1);
        this.changeText();
    }
    changeText() {
        let action = this.list.children;
        let ads = game.playerMgr.ads;
        action.forEach((val, idx) => {
            val.getChildByName("leftCount").getComponent(cc.RichText).string = `剩余次数：${ads[idx]}`;
            if(ads[idx] <= 0 ) {
                val.getChildByName("leftCount").color =  cc.Color.RED;
            }
        })
    }
}
